package AI;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import topics.*;

/**
 * this class decodes the lesson into an array of main topics
 * that containg sub topics and microtopics.
 * @author link3988
 */
public class LessonDecoder {

    File file; //file to decode
    ArrayList<topics.MainTopic> topiclist; //array list to be created.
    Topic lastTopic;
    public LessonDecoder(File flnm)
    {
        file = flnm;
        lastTopic = null;
    }

    /**
     * does all the decoding from the file and looks for keywords
     * and performs the appropriate action
     * @return returns the arraylist of main topics.
     */
    public ArrayList<topics.MainTopic> decode()
    {
        topiclist = new ArrayList();
        try
        {
            BufferedReader inputFile = new BufferedReader( new FileReader(file) );
            String line = inputFile.readLine();
           while (!(line.compareTo("start")==0))
           {
                line = inputFile.readLine();
           }
           line = inputFile.readLine();
           topics.MainTopic lastMain = null;
           topics.SubTopic lastSub = null;
           while (line !=null && !(line.compareTo("stop")==0))
                    {
                        StringTokenizer st = new StringTokenizer(line, " ");
                        String temp = st.nextToken();                        

                        //creation of new topics.
                        if(temp.compareTo("new")==0)
                        {
                                temp = st.nextToken();
                                String top = "";
                                if(temp.compareTo("topic")==0)//new main topic
                                {
                                        top = line.substring(10);
                                        topics.MainTopic topic = new MainTopic(top);
                                        lastMain = topic;
                                        topiclist.add(topic);
                                        lastTopic = topic;
                                }
                                else if(temp.compareTo("subtopic")==0)//new subtopic
                                {
                                        top = line.substring(13);
                                        topics.SubTopic topic = new SubTopic(top);
                                        lastSub = topic;
                                        lastMain.addSubTopic(topic);
                                        lastTopic = topic;
                                }
                                else if(temp.compareTo("microtopic")==0)//new micro topic
                                {
                                        top = line.substring(15);
                                        topics.MicroTopic topic = new MicroTopic(top);
                                        lastSub.addMicroTopic(topic);
                                        lastTopic = topic;
                                }
                        }
                        //more detail
                        else if(temp.compareTo("more")==0)
                        {
                            temp = st.nextToken();
                                if(temp.compareTo("detail")==0)
                                {
                                    String whattoadd = line.substring(12);
                                    append(whattoadd);
                                }
                                else
                                {
                                        System.out.println("invalid input format");
                                }
                        }

                        else if(temp.compareTo("append")==0)
                        {
                            String whattoadd = line.substring(7);
                                    append(whattoadd);
                        }

                        else if(temp.compareTo("back")==0)//pause and wait for resume
                        {
                                temp = st.nextToken();
                                if(temp.compareTo("to")==0)
                                {
                                    String tothis = line.substring(8);
                                    lastTopic = (Topic) findTopic(tothis);
                                    if(lastTopic instanceof MainTopic)
                                    {
                                        lastMain = (MainTopic) lastTopic;
                                    }
                                    else if(lastTopic instanceof SubTopic)
                                    {
                                        lastSub = (SubTopic) lastTopic;
                                    }
                                }
                        }
                        else if(temp.compareTo("write")==0)//overwrites the topic
                        {
                            String changeto = line.substring(6);
                                        lastTopic.setTopic(changeto);
                        }

                        else if(temp.compareTo("pause")==0)//pause and wait for resume
                        {
                                while (line !=null && !(line.compareTo("resume")==0))
                                {
                                        line = inputFile.readLine();
                                }
                        }
                        line = inputFile.readLine();
           }
        inputFile.close();
        }catch(Exception e ){System.out.println(e);}

        return topiclist;
    }

    /**
     * Returns the topic with the specified topic.
     * @param tothis
     * @return
     */
    public Object findTopic(String tothis)
    {
        Object found = null;
        for(int i = 0; i < topiclist.size(); i ++)
        {
            MainTopic temp = topiclist.get(i);
            if(temp.getTopic().equals(tothis))
            {
                return temp;
            }
            for(int j = 0; j < temp.subSize(); j++)
            {
                SubTopic tempsub = temp.getSubTopic(j);
                if(tempsub.getTopic().equals(tothis))
                {
                    return tempsub;
                }
                for(int k = 0; k < tempsub.microSize(); k++)
                {
                    MicroTopic tempmicro = tempsub.getMicroTopic(k);
                    if(tempmicro.getTopic().equals(tothis))
                    {
                        return tempmicro;
                    }
                }
            }
        }
        return found;
    }

    /**
     * Appends whattoadd to the last topic.
     * @param whattoadd
     */
    public void append(String whattoadd)
    {
        lastTopic.addToTopic(whattoadd);
    }
}
